package com.souku.finance.application.ui.invite;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.gc.materialdesign.views.ProgressBarDeterminate;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.InviteBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.dialog.CalculationDialog;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.PayUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.CircleViewHome;
import com.souku.finance.application.widget.MyScrollView;
import com.souku.finance.application.widget.MyTextView;
import com.souku.finance.application.widget.PullUpToLoadMore;
import com.souku.finance.application.widget.TouchWebView;
import com.souku.finance.application.widget.ViewPagerIndicator;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/19.
 */

public class ShouKuTenderDetailsActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ImageView ivCalculation;
    protected CircleViewHome circle;
    protected TextView tvCircle;
    protected MyScrollView scrollViewBottom;
    protected PullUpToLoadMore ptlm;
    protected ProgressBar seekbar;
    protected TextView tvGototender;
    protected ViewPagerIndicator idIndicator;
    protected ViewPager idVp;
    protected ProgressBarDeterminate progressDeterminate;
    protected MyTextView tvPoint;
    protected TextView tv1;
    protected TextView tv2;
    protected TextView tv3;
    protected TextView tv4;
    protected TextView tv7;
    protected TextView tv5;
    protected TextView tv8;
    protected TextView tv6;
    protected TextView tv9;
    protected TextView tvLimit;
    protected TextView tv10;
    protected TextView tvMinmoney;
    protected LinearLayout linearlayoutHide;
    protected LinearLayout linearlayoutCircle;
    protected TextView textView;
    protected TouchWebView webView;
    private String id = "";
    private InviteBean bean;

    @Override
    public void internet() {
        getDeal_detail(id);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDeal_detail(id);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_tenderdetails);
        id = getIntent().getStringExtra(Constant.Default);
        initView();
        circle.setStoke(this);
        topTitle.setText("标名");
        setWebview(webView);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            onBackPressed();
        } else if (view.getId() == R.id.iv_calculation) {
            if (bean == null) return;
//            new CalculationDialog(this, new CalculationDialog.CalculationOnClick() {
//                @Override
//                public void ok(EditText et1, EditText et2, EditText et3) {
//
//                    String s1 = et1.getText().toString().trim();
//
//                    String s2 = et2.getText().toString().trim();
//
//                    if (MyTextUtil.isEmpty(s1) || Integer.parseInt(s1) == 0) {
//                        ToastUtil.show("请输入投资金额");
//                        return;
//                    }
//
//                    if (Integer.parseInt(s1) < 100) {
//                        ToastUtil.show("投资金额不得少于100元");
//                        return;
//                    }
//
//                    if (MyTextUtil.isEmpty(s2) || Integer.parseInt(s2) == 0) {
//                        ToastUtil.show("请输入理财期限");
//                        return;
//                    }
//
//                    getCal_interest(Integer.parseInt(s1), s2, et3);
//
//                }
//            }, bean.getIsCurrentDeal().equals("1"), Double.parseDouble(bean.getRate())).show();

            Intent intent = new Intent(ShouKuTenderDetailsActivity.this, CalculationActivity.class);
            intent.putExtra("isCurrentDeal", bean.getIsCurrentDeal().equals("1"));
            intent.putExtra("rate", Double.parseDouble(bean.getRate()));
            startActivity(intent);

        } else if (view.getId() == R.id.tv_gototender) {
            if (bean == null) {
                getDeal_detail(id);
                return;
            }
            PayUtil.check(this, id, bean.getIsNew().equals("1"));
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(ShouKuTenderDetailsActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        ivCalculation = (ImageView) findViewById(R.id.iv_calculation);
        ivCalculation.setOnClickListener(ShouKuTenderDetailsActivity.this);
        circle = (CircleViewHome) findViewById(R.id.circle);
        tvCircle = (TextView) findViewById(R.id.tv_circle);
        scrollViewBottom = (MyScrollView) findViewById(R.id.scroll_view_bottom);
        ptlm = (PullUpToLoadMore) findViewById(R.id.ptlm);
        seekbar = (ProgressBar) findViewById(R.id.seekbar);
        tvGototender = (TextView) findViewById(R.id.tv_gototender);
        tvGototender.setOnClickListener(ShouKuTenderDetailsActivity.this);
        idIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
        idVp = (ViewPager) findViewById(R.id.id_vp);
        progressDeterminate = (ProgressBarDeterminate) findViewById(R.id.progressDeterminate);
        tvPoint = (MyTextView) findViewById(R.id.tv_point);
        tv1 = (TextView) findViewById(R.id.tv_1);
        tv2 = (TextView) findViewById(R.id.tv_2);
        tv3 = (TextView) findViewById(R.id.tv_3);
        tv4 = (TextView) findViewById(R.id.tv_4);
        tv7 = (TextView) findViewById(R.id.tv_7);
        tv5 = (TextView) findViewById(R.id.tv_5);
        tv8 = (TextView) findViewById(R.id.tv_8);
        tv6 = (TextView) findViewById(R.id.tv_6);
        tv9 = (TextView) findViewById(R.id.tv_9);
        tvLimit = (TextView) findViewById(R.id.tv_limit);
        tv10 = (TextView) findViewById(R.id.tv_10);
        tvMinmoney = (TextView) findViewById(R.id.tv_minmoney);
        linearlayoutHide = (LinearLayout) findViewById(R.id.linearlayout_hide);
        linearlayoutCircle = (LinearLayout) findViewById(R.id.linearlayout_circle);
        textView = (TextView) findViewById(R.id.textView);
        webView = (TouchWebView) findViewById(R.id.web_view);
    }

    private void setWebview(final TouchWebView web_view) {
        web_view.setPtlm(ptlm);
        web_view.setBackgroundColor(2);
        progressDeterminate.setProgress(100);
        web_view.getSettings().setJavaScriptEnabled(true);
        web_view.getSettings().setBlockNetworkImage(false);
        web_view.getSettings().setLoadWithOverviewMode(true);
        web_view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        web_view.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressDeterminate.setProgress(newProgress);
                if (newProgress == 100) {
                    progressDeterminate.setVisibility(View.GONE);
                }
            }
        });
        web_view.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (progressDeterminate.getVisibility() == View.VISIBLE) {
                    progressDeterminate.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (progressDeterminate.getVisibility() == View.GONE) {
                    progressDeterminate.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (ptlm.getScrollY() != 0) {
            ptlm.scrollToTop();
        } else {
            super.onBackPressed();
        }
    }


    private void getDeal_detail(String id) {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.INDEX, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), InviteBean.class);
                    bean.setIsCurrentDeal("1");
                    bean.setIsNew("0");
                    setData();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void getCal_interest(int money, String duration, final EditText et3) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("money", money * 100 + "");
        map.put("duration", duration);
        RequestManager.getInstance().post(UrlConstact.CAL_INTEREST, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    et3.setText(MyTextUtil.setMoneyToElement(JsonUtil.getString(JsonUtil.getJSONObject(response), "data")));
                } else {
                    et3.setText("错误");
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void setData() {
        tvPoint.setText(bean.getRate());
        tv7.setVisibility(View.VISIBLE);
//        webView.loadUrl(bean.getDetailHtml5());
//        webView.loadUrl("http://m.91skjr.com/sk_baby/" + id);
        webView.loadUrl("http://m.91skjr.com/skbao/" + id);

        topTitle.setText(bean.getDealName());
        if (bean.getIsCurrentDeal().equals("1")) {
            tv1.setVisibility(View.GONE);
            tv2.setVisibility(View.VISIBLE);
            tv2.setText("在投本金满2000元，每10天利率上涨0.1%");
            tv3.setVisibility(View.GONE);
            tv4.setVisibility(View.VISIBLE);
//            tv4.setText("随存随取");
            tv4.setText("一个月");
            tv5.setVisibility(View.VISIBLE);
            tv5.setText(MyTextUtil.setMoneyToElement(bean.getMinInvestment()) + "元起投");
            tv6.setVisibility(View.VISIBLE);
//            tv6.setText("次日计息");
            tv6.setText("到期计息");
            tv8.setVisibility(View.VISIBLE);
            tv8.setText("起投金额");
            tv9.setVisibility(View.VISIBLE);
            tv9.setText("计息");
            linearlayoutHide.setVisibility(View.GONE);
//            circle.setPrograss(Integer.parseInt(bean.getBorrowAmount()), Integer.parseInt(bean.getBorrowAmount()), tvCircle);
            circle.setPrograss(100, 100, tvCircle);
        } else {
            tv1.setVisibility(View.VISIBLE);
            tv1.setGravity(Gravity.LEFT);
            if (bean.getUseEcv().equals("1")) {
                tv1.setText("可用红包");
            } else {
                tv1.setText("不可用红包");
            }
            tv2.setVisibility(View.VISIBLE);
            if (bean.getUseRate().equals("1")) {
                tv2.setText("可用加息券");
            } else {
                tv2.setText("不可用加息券");
            }
            if (MyTextUtil.isEmpty(bean.getActivityName())) {
                tv3.setVisibility(View.GONE);
                tv1.setGravity(Gravity.CENTER);
            } else {
                tv3.setVisibility(View.VISIBLE);
                tv3.setText(bean.getActivityName());
            }
            tv4.setVisibility(View.VISIBLE);
            if (bean.getDealDateType().equals("1")) {
                tv4.setText(Integer.parseInt(bean.getDealDate()) * 30 + "天");
            } else {
                tv4.setText(Integer.parseInt(bean.getDealDate()) + "天");
            }
            tv5.setVisibility(View.VISIBLE);
            tv5.setText(MyTextUtil.changeTimeFormat(bean.getStartTime()));
            tv6.setVisibility(View.VISIBLE);
            tv6.setText(MyTextUtil.changeTimeFormat(bean.getLoanTime()));
            tv8.setVisibility(View.VISIBLE);
            tv8.setText("开标时间");
            tv9.setVisibility(View.VISIBLE);
            tv9.setText("到期时间");
            linearlayoutHide.setVisibility(View.VISIBLE);
            seekbar.setProgress(Integer.parseInt(bean.getInvestmentAmount()));
            seekbar.setMax(Integer.parseInt(bean.getBorrowAmount()));
            tvLimit.setText(MyTextUtil.setMoneyToElement(Integer.parseInt(bean.getBorrowAmount()) - Integer.parseInt(bean.getInvestmentAmount()) + ""));
            tv10.setText("已售" + MyTextUtil.doubleFormat(Integer.parseInt(bean.getInvestmentAmount()) * 100.0 / Integer.parseInt(bean.getBorrowAmount())) + "%");
            circle.setPrograss(Integer.parseInt(bean.getInvestmentAmount()), Integer.parseInt(bean.getBorrowAmount()), tvCircle);
            if (bean.getMinInvestment().equals("0")) {
                tvMinmoney.setText("不限");
            } else {
                tvMinmoney.setText(MyTextUtil.setMoneyToElement(bean.getMinInvestment()));
            }
        }
    }
}
